package bma.common.langutil.log.acclog;

import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class AccessLogAbstract implements AccessLog {

	private long id;
	private char separator = ',';
	private char separatorReplace = ',';

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public char getSeparator() {
		return separator;
	}

	public void setSeparator(char separator) {
		this.separator = separator;
	}

	public char getSeparatorReplace() {
		return separatorReplace;
	}

	public void setSeparatorReplace(char separatorReplace) {
		this.separatorReplace = separatorReplace;
	}

	@Override
	public String parse(String s) {
		if (s == null)
			return s;
		return s.replace(separator, separatorReplace);
	}

	public String build(Date time, String level, String ltag, Object[] args) {
		StringBuilder sb = new StringBuilder();
		SimpleDateFormat sdf = new SimpleDateFormat(LOG_TIME_FORMAT);
		if (time != null) {
			sb.append(sdf.format(time)).append(separator);
		}
		if (level != null) {
			sb.append(level).append(separator);
		}
		sb.append(id).append(separator);
		sb.append(ltag);
		for (int i = 0; i < args.length; i++) {
			sb.append(separator);
			sb.append(args[i]);
		}
		return sb.toString();
	}

	protected abstract void write(String s);

	@Override
	public void debug(String ltag, Object... args) {
		if (isDebugEnabled()) {
			write(build(new Date(), "DEBUG", ltag, args));
		}
	}

	@Override
	public void info(String ltag, Object... args) {
		if (isInfoEnabled()) {
			write(build(new Date(), "INFO", ltag, args));
		}
	}

	@Override
	public void warn(String ltag, Object... args) {
		if (isWarnEnabled()) {
			write(build(new Date(), "WARN", ltag, args));
		}
	}

}
